package com.lxj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxj.domain.Prod;
import com.lxj.mapper.ProdMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxj.mapper.ProdCommMapper;
import com.lxj.domain.ProdComm;
import com.lxj.service.ProdCommService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * @author lxj
 * @date 2021/8/1 11:11
 * @Description: 
 */
@Service
@Slf4j
public class ProdCommServiceImpl extends ServiceImpl<ProdCommMapper, ProdComm> implements ProdCommService{

    @Autowired
    private ProdCommMapper prodCommMapper;

    @Autowired
    private ProdMapper prodMapper;

    /**
     * 分页查询评论
     *
     * @param page
     * @param prodComm
     * @return
     */
    @Override
    public Page<ProdComm> loadProdCommPage(Page<ProdComm> page, ProdComm prodComm) {
        // 先判断有没有传名字进来
        String prodName = prodComm.getProdName();
        List<Long> prodIds = null;
        if(StringUtils.hasText(prodName)){
            // 查询商品表
            List<Prod> prods = prodMapper.selectList(new LambdaQueryWrapper<Prod>()
                    .like(Prod::getProdName, prodName)
            );
            if(!CollectionUtils.isEmpty(prods)){
                // 拿到prodIds
                prodIds = prods.stream().map(Prod::getProdId).collect(Collectors.toList());
            }else {
                // 直接返回
                page.setRecords(Collections.emptyList());
                page.setTotal(0L);
                return page;
            }
        }
        // 分页查询评论表
        Page<ProdComm> prodCommPage = prodCommMapper.selectPage(page, new LambdaQueryWrapper<ProdComm>()
                .eq(!ObjectUtils.isEmpty(prodComm.getStatus()), ProdComm::getStatus, prodComm.getStatus())
                .in(!CollectionUtils.isEmpty(prodIds), ProdComm::getProdId, prodIds)
                .orderByDesc(ProdComm::getRecTime)
        );
        //组装名字
        List<ProdComm> prodComms = prodCommPage.getRecords();
        if(CollectionUtils.isEmpty(prodComms)){
            // 说明数据库里面还没有评论
            return prodCommPage;
        }
        //精确查询 根据查到的评论集合反向拿到商品的ids
        List<Long> finalProdIds = prodComms.stream().map(ProdComm::getProdId).collect(Collectors.toList());
        // 再查询商品表
        List<Prod> prods = prodMapper.selectBatchIds(finalProdIds);
        // 循环评论
        prodComms.forEach(prodComm1 -> {
            Prod prod1 = prods.stream()
                    //过滤出商品id相同的商品
                    .filter(prod -> prod.getProdId().equals(prodComm1.getProdId()))
                    .collect(Collectors.toList())
                    .get(0);
            //设置名字
            prodComm1.setProdName(prod1.getProdName());
        });
        return prodCommPage;
    }
}
